﻿using EmitMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Core;
using WebSite.Data.NH.Repositories;
using WebSite.Model;
using WebSite.Web.Models;
using WebSite.Web.SessionAndFilter;

namespace WebSite.Web.Controllers.Admin
{
    [AuthorAttribute]
    public class AdvController : Controller
    {
        private readonly IAdvRepository advr;

        public AdvController(IAdvRepository advr)
        {
            this.advr = advr;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int Id = 0)
        {
            AdvVM adv = null;
            if (Id == 0)
            {
                adv = new AdvVM();
                adv.CompanyId = Sessions.User.CompanyId;
            }
            else
            {
                ObjectsMapper<AdvInfo, AdvVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<AdvInfo, AdvVM>();
                AdvInfo advi = advr.GetAdv(Id);
                adv = mapper.Map(advi);
            }
            return View(adv);
        }

        [HttpPost]
        public ActionResult Edit(AdvVM av)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<AdvVM, AdvInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<AdvVM, AdvInfo>();

                var user = Sessions.User;

                if (av.Id == 0)
                {
                    var a = mapper.Map(av);
                    bool res = advr.AddAdv(a);
                    return Json(res);
                }
                else
                {
                    var a = mapper.Map(av);
                    bool res = advr.UpdateAdv(a);
                    return Json(res);
                }

            }
            return View();
        }
        

        [HttpPost]
        public JsonResult Advs(string title,int page = 1, int pageSize = 15 )
        {
            var user = Sessions.User;

            GridData<AdvVM> data = new GridData<AdvVM>();

            ObjectsMapper<AdvInfo, AdvVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<AdvInfo, AdvVM>();

            var where = PredicateExtension.True<AdvInfo>();
            where = where.AndAlso(u => u.CompanyId == user.CompanyId);
            if (!string.IsNullOrEmpty(title))
            {
                where = where.AndAlso(u => u.Title.Contains(title.Trim()));
            }

            var count = advr.LoadCount(where);
            var list = advr.LoadAll(where, page, pageSize, new OrderByExpression<AdvInfo, int>(u => u.Id, true));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        [HttpPost]
        public ActionResult DeleteAdv(int id)
        {
            var b = false;
            b = advr.DeleteAdv(id);
            return Json(b);
        }

    }
}
